﻿using System;
using Draco.DataStructure;

namespace BusinessEntities
{
    public class MetroPath
    {
        public D_LinkList<MetroLink> Links { get; private set; }

        public int Transfers { get; private set; }

        private int distance = 0;
        private int time = 0;

        public MetroPath()
        {
            Links = new D_LinkList<MetroLink>();
        }

        public void AddLinks(MetroLink link)
        {
            Links.Add(link);
            distance += link.Distance;
            time += link.Time;
        }

        public int GetDistance()
        {
            if (Links.Count == 0) return Int32.MaxValue;
            return distance;
        }

        public int GetTime()
        {
            if (Links.Count == 0) return Int32.MaxValue;
            return time + Transfers * 7;  //假设换乘时间为7分钟
        }

        public float GetPrice()
        {
            float price = 0;
            if (GetDistance() < 200)
            {
                price = 2;
            }
            else
            {
                price = 2 + (GetDistance()-200)/100.0f;
            }

            return price;
        }

        public MetroNode GetStartNode()
        {
            if (Links.Count > 0)
            {
                return Links[0].From;
            }
            return null;
        }

        public MetroNode GetEndNode()
        {
            if (Links.Count > 0)
            {
                return Links[Links.Count - 1].To;
            }
            return null;
        }


        /// <summary>
        /// 合并两条不同线路径
        /// </summary>
        /// <param name="path">第二条路径</param>
        public void Merge(MetroPath path)
        {
            if (path.Links.Count > 0)
            {
                if (GetEndNode() == path.GetStartNode())
                {
                    Transfers++;
                }

                foreach (MetroLink link in path.Links)
                {
                    AddLinks(link);
                }
            }
        }

        public MetroPath Clone()
        {
            MetroPath path = new MetroPath();
            foreach (MetroLink link in Links)
            {
                path.AddLinks(link);
            }
            path.Transfers = Transfers;
            return path;
        }

        public void Clear()
        {
            Links.Clear();
            time = 0;
            distance = 0;
            Transfers = 0;
        }

        public override string ToString()
        {
            if (Links.IsEmpty()) return "没有路径";
            string str = "";
            str += Links[0].From.Name + "->" + Links[0].To.Name;
            for (int i = 1; i < Links.Count; i++)
            {
                str += "->" + Links[i].To.Name;
            }
            str += "  ||  换乘：" + Transfers + "次";
            str += "  ||  耗时：" + GetTime() + "分";
            str += "  ||  距离：" + GetDistance() + "米";
            str += "  ||  票价：" + GetPrice() + "元";
            return str;
        }
    }
}
